define(['request', 'template', 'jquery', 'bootstrap', 'jq_cookie'], (request, template) => {
    class Header {
        constructor() {
            this.fixheader().then(() => {
                this.inputsearch()
                this.nav_show_or_hide()
                this.loaded_show()
                this.load_reg()
                this.uesr_load()
                this.user_reg()
                this.shopnumbers()
                this.isstop() 
            })
        }
        fixheader() { //获取加载内容并加载
            return new Promise((t) => { //返回一个加载成功的承诺
                $('header').load('/html/modules/header.html', t)
            })
        }
        inputsearch() { //搜索功能，获取输入内容
            $('#inputsearch').on('focus', () => { //得到焦点时移出a,处理value
                $('#inputsearch_a').remove()
                $('#inputsearch').on('keyup', () => { //鼠标抬起活动value
                    console.log($('#inputsearch').val()) //暂时没有提交，只有console.log，可以做输入提示
                })
            }).on('blur', () => {
                $('#inputsearch_father').append('<a href="#" id ="inputsearch_a" >阿狸</a>') //失去焦点时添加
            })
        }
        get_index_nav() { //首页导航栏
            return new Promise((t) => {
                request.get_index_nav().then((resp) => {
                    if (resp.code === 200) {
                        const list = resp.body //{list:[{},{},{}]}
                        this.show_hide = list.list
                        $('#header-nav-ul').html(template('header-nav-li', list))
                        this.showhide = list.list
                        t()
                    }
                })
            })
        }
        nav_show_or_hide() { //是否加载首页>其它页面
            this.get_index_nav().then(() => {
                if ($('#nav_show_or_hide').length) { //有这个栏才会显示
                    var hid = location.hash.slice(1)
                    var str = `<div class="detial_center_nav">
                        <a href="/index.html">商城首页</a>
                        <span>></span>
                        <span>${this.showhide[hid-1].ctitle}</span>
                        </div>`
                    $('#nav_show_or_hide').html(str)
                }
            })
        }

        load_reg() { //登陆注册切换
            $('.modal-title').on('click', function () {
                $('.modal-title').removeClass('you_modal-title-color')
                $(this).addClass('you_modal-title-color')
                let index = $('.modal-title').index($(this))
                $('.form-hide-show').removeClass('form-hide').eq(1 - index).addClass('form-hide')
                $('.btn-primary').removeClass('form-hide').eq(1 - index).addClass('form-hide')
            })
        }

        uesr_load() { //登陆设置cookie
            var _this = this
            $('#btn-load').on('click', function () {
                _this.putusername = $('#load_user_name').val()
                let putpassword = $('#load_user_password').val()
                if ($.cookie(_this.putusername) === undefined) {
                    $('.modal-footer').before('<div class="alert alert-danger" role="alert">"用户名不存在"</div>')
                    var danger = setTimeout(() => {
                        $('.alert-danger').remove()
                    }, 2000)
                } else {
                    let password = JSON.parse($.cookie(_this.putusername)).password
                    if (putpassword === password) { //验证密码正确
                        $.cookie('ison', _this.putusername, {
                            expires: 7,
                            path: '/'
                        }) //加入登录状态
                        _this.loaded_show()
                    } else {
                        $('.modal-footer').before('<div class="alert alert-danger" role="alert">"密码错误"</div>')
                        var dangereo = setTimeout(() => {
                            $('.alert-danger').remove()
                        }, 2000)
                    }
                }
            })
        }
        user_reg() { //注册
            var _this = this
            $('#btn-reg').on('click', function () {
                let username = $('#reg_user_name').val()
                let password = $('#reg_user_password').val()
                //验证用户名是否存在

                if ($.cookie(username) !== undefined) { //姓名是否重复
                    $('.modal-footer').before('<div class="alert alert-danger" role="alert">"用户名已存在"</div>')
                    var eor = setTimeout(() => {
                        $('.alert-danger').remove()
                    }, 2000)
                } else {
                    $.cookie(username, JSON.stringify({
                        'password': password
                    }), {
                        expires: 360,
                        path: '/'
                    }) //存入cookie
                    $.cookie('ison', username, {
                        expires: 7,
                        path: '/'
                    }) //加入登录状态
                    _this.loaded_show()
                }
            })
        }

        loaded_show() { //是否已经登陆判断操作
            if ($.cookie('ison')) {
                $('#modal-hellow').hide().before
                $('#logoff').html(`${$.cookie('ison')},注销`).show()
                $('#myModal').modal('hide') //关闭模态框
                $('#logoff').on('click', () => {
                    $('#modal-hellow').show();
                    $('#logoff').hide()
                    $.cookie('ison', null, {
                        expires: -1,
                        path: '/'
                    }) //删除登录标记
                })
            }
        }

        shopnumbers() { //头部导航栏数量
            var shop = document.querySelector('#shopnumbers'),
                sum = 0
            var arr = JSON.parse(localStorage.getItem($.cookie('ison')))
            if(arr){
            arr.forEach((i) => {
                sum += i.num
            })
            shop.innerHTML = sum
        }
        }

        isstop() { //未登录，阻止访问购物车
            $('#isstop').on('click',function(event){
                if ($.cookie('ison') === undefined) {
                    event.preventDefault()//如果未登录，报错
                    alert('请先登陆') //如果未登录，报错
                }
            })
        }
    }
    return new Header()
});